# Maintainer: Andrei Zavada <andrei.zavada@tiot.jp>

# This APKBUILD is based on community/erlang maintained by Daniel
# Isaksen <d@duniel.no>. See ../erlang/APKBUILD for the list of
# contributors.

# The reason for this build is that Erlang projects often require a
# specific version of OTP (and not the latest). Case in point is riak,
# which cannot be built with the latest OTP.

pkgname=erlang22
pkgver=22.3.4.24
pkgrel=0
pkgdesc="General-purpose programming language and runtime environment (version 22)"
url="https://www.erlang.org/"
license="Apache-2.0"
arch="all !riscv64" # ftbfs
makedepends="perl-dev perl zlib-dev ncurses-dev openssl1.1-compat-dev
	autoconf"
subpackages="$pkgname-dev
	$pkgname-debugger:_mv_erlang_lib
	$pkgname-dialyzer:_mv_erlang_lib
	$pkgname-reltool:_mv_erlang_lib
	"
source="https://github.com/erlang/otp/releases/download/OTP-$pkgver/otp_src_$pkgver.tar.gz"

builddir="$srcdir/otp_src_$pkgver"

prepare() {
	default_prepare
	update_config_sub
}

build() {
	export CPPFLAGS="-D_BSD_SOURCE $CPPFLAGS"

	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--host="$CHOST" \
		--build="$CBUILD" \
		--enable-threads \
		--enable-shared-zlib \
		--enable-ssl=dynamic-ssl-lib \
		--without-javac
	make
}

check() {
	ERL_TOP="$builddir" make release_tests
}

package() {
	make DESTDIR="$pkgdir" install
}

_mv_erlang_lib() {
	local lib=$(echo ${subpkgname#"$pkgname-"} | tr '-' '_')
	depends="$pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/lib/erlang/lib
	rm -f "$pkgdir"/usr/lib/erlang/lib/$lib-*/src/*.erl
	mv "$pkgdir"/usr/lib/erlang/lib/$lib-* "$subpkgdir"/usr/lib/erlang/lib/
}

dev() {
	set -x
	local i=''
	depends="$pkgname=$pkgver-r$pkgrel $depends_dev"
	pkgdesc="$pkgdesc (development files)"

	cd "$pkgdir"
	local libdirs=usr/
	[ -d lib/ ] && libdirs="lib/ $libdirs"
	for i in usr/include usr/lib/pkgconfig usr/share/aclocal\
			usr/share/gettext usr/bin/*-config		\
			$(find . -name include -type d)			\
			$(find $libdirs -name '*.[acho]'		\
			-o -name '*.prl' 2>/dev/null); do

		if [ -e "$pkgdir/$i" ] || [ -L "$pkgdir/$i" ]; then
			d="$subpkgdir/${i%/*}"  # dirname $i
			mkdir -p "$d"
			mv "$pkgdir/$i" "$d"
			rmdir "$pkgdir/${i%/*}" 2>/dev/null || true
		fi
	done

	# move *.so links needed when linking the apps to -dev packages
	for i in lib/*.so usr/lib/*.so; do
		if [ -L "$i" ]; then
			mkdir -p "$subpkgdir"/"${i%/*}"
			mv "$i" "$subpkgdir/$i"
		fi
	done
}

# helper script to manually check the dependencies of subpackages
verifydeps() {
	local _p
	for _p in $subpackages; do
		case $_p in
			*:*) _p=${_p%:*};;
			*) continue;;
		esac
		local _subdir="$pkgdir/../$_p"
		local _pkginfo="$pkgdir/../.control.$_p"/.PKGINFO
		msg "$_p"
		grep '^depend ='  $_pkginfo || true
		find "$_subdir" -name '*.app' | while read -r _f; do
			echo "  $_f"
			sed -n '/{runtime_dependencies/,/}/p' $_f
		done
	done
}

sha512sums="
af75eaea92aa47519d3e35b76f1996ee5a1de4c5812d3f42aba493301f1e83cb422466c3c61dbea7eac3599eb0054ef9b3a12061122ba7fa5284124b608233c3  otp_src_22.3.4.24.tar.gz
"
